<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Item extends Model
{
    //状态：状态：0无效，1有效中，2结束
    const STATUS_INVALID = 0;
    const STATUS_VALID = 1;
    const STATUS_FINISH = 2;

    protected $fillable = [
        'company_id',
        'project_id',
        'publisher_uid',
        'department',
        'title',
        'stage',
        'price',
        'expired_at',
    ];

    protected $appends = [
        'description',
    ];

    public function contact()
    {
        return $this->hasOne(ItemHasContact::class);
    }

    public function company()
    {
        return $this->belongsTo(Company::class);
    }

    public function project()
    {
        return $this->belongsTo(Project::class);
    }

    public function extend()
    {
        return $this->hasOne(ItemExtend::class);
    }

    public function publisher()
    {
        return $this->belongsTo(User::class, 'publisher_uid');
    }

    public function getDescriptionAttribute()
    {
        return $this->extend ? $this->extend->description : '';
    }

    public function stage()
    {
        return $this->belongsTo(ItemStage::class, 'stage');
    }

    public function tags()
    {
        return $this->belongsToMany(Tag::class, 'item_has_tags')->withTimestamps();
    }

    /**
     * 是否有效
     *
     * @return bool
     */
    public function isValid()
    {
        return $this->status == self::STATUS_VALID;
    }
}
